﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DnnDash.SimplePhoneApp.Common;
using DnnDash.SimplePhoneApp.ViewModel;

namespace DnnDash.SimplePhoneApp
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        private AppSettings appSettings;

        public SettingsPage()
        {
            InitializeComponent();

            // Get access to our settings
            appSettings = new AppSettings();
            appSettings.OnPing += new AppSettings.OnPingHandler(appSettings_OnPing);
            // Current settings into the text boxes
            uiUsername.Text = appSettings.Username;
            uiPassword.Password = appSettings.Password;
            uiDomain.Text = appSettings.DomainAlias;
        }

        void appSettings_OnPing(object sender, PingEventArgs e)
        {
            string statusSummary;
            string result = e.PingResult;
            if (result == null)
                result = "Empty Ping Result";
            switch (e.StatusCode)
            {
                case HttpStatusCode.OK:
                    statusSummary = "Connection OK";
                    break;
                case HttpStatusCode.NotFound:
                    statusSummary = "Connection failed with 404";
                    break;
                case HttpStatusCode.Unauthorized:
                case HttpStatusCode.Forbidden:
                    statusSummary = "Not Authorised to access service";
                    break;
                case HttpStatusCode.InternalServerError:
                    statusSummary = "Server Error Returned";
                    break;
                default:
                    statusSummary = "Status :" + e.StatusCode.ToString();
                    break;
            }
            if (e.CallName == "Ping")
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Anonymous connection result : " + statusSummary, result, MessageBoxButton.OK);
                });
            }
            if (e.CallName == "PingHost")
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Authenticated connection result : " + statusSummary, result, MessageBoxButton.OK);
                });
            }
        }

        private void OnAppBarDoneClick(object sender, EventArgs e)
        {
            appSettings.Username = uiUsername.Text;
            appSettings.Password = uiPassword.Password;
            appSettings.DomainAlias = uiDomain.Text;

            if (NavigationService.CanGoBack)
                NavigationService.GoBack();
        }

        private void OnAppBarCancelClick(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void OnTestConnectionClick(object sender, RoutedEventArgs e)
        {
            try
            {
                appSettings.PingService();
            }
            catch (Exception ex)
            {
                //TODO: handle error?
            }
        }

    }
}